package com.example.petadopt.controller;

import com.example.petadopt.controller.vo.Result;
import com.example.petadopt.dao.ImgDao;
import com.example.petadopt.entity.Img;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/imgs")
public class ImgController {
    @Autowired
    private ImgDao imgDao;

    @PostMapping("/add")
    public Result addImg(@RequestBody List<Img> imgData) {
        try {
            imgDao.saveAll(imgData);
            return new Result(2000, "图片信息提交成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(5000, "图片信息提交失败", e.getMessage());
        }
    }

    @RequestMapping("/getImg")
    public Result getImg(int pet_id) {
        List<Img> imgList = imgDao.findByImgPetId(pet_id);
        List<String> imgs= imgList.stream().map(Img::getImgUrl).collect(Collectors.toList());
        return new Result(2000, "获取图片成功", imgs);
    }

}
